package com.macro.mall.portal.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

/**
 * @Author：尚志豪
 * @Package：com.macro.mall.portal.config
 * @Project：mall-swarm-B
 * @name：AsyncConfig
 * @Date：2024/10/31 17:24
 */
@Component
public class AsyncConfig {

    @Bean("AsyncExecutor")
    public ThreadPoolTaskExecutor poolTaskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //设置核心线程
        executor.setCorePoolSize(8);
        //设置最大线程组
        executor.setMaxPoolSize(20);
        //设置队列比例
        executor.setQueueCapacity(200);
        //设置线程活跃时间
        executor.setKeepAliveSeconds(60);
        // 设置线程名前缀+分组名称
        executor.setThreadNamePrefix("AsyncOperationThread-");
        executor.setThreadGroupName("AsyncOperationGroup");
        // 所有任务结束后关闭线程池
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 初始化
        executor.initialize();
        return executor;
    }
}
